﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UpdateDesktopDocHtmlContent
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //File.WriteAllText("D:\\Program Files\\SendMailServer\\u1.txt", DateTime.Now.ToLongTimeString());
            if (args.Length > 0)
            {
                //File.WriteAllText("D:\\Program Files\\SendMailServer\\u.txt", DateTime.Now.ToLongTimeString());
                string[] fileNames = File.ReadAllText("D:\\Program Files\\SendMailServer\\batchExe.txt").Split(',');
                var form = new Form1();
                form.Form1_Load(null, null);
                form.LastSelectedDirectory = fileNames[2];
                form.IsSelience = true;
                form.ProcessDirectory();
                return;
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }


        private static void TT()
        {
            StreamWriter streamWriter = File.CreateText("D:\\Program Files\\SendMailServer\\log.txt");

            try
            {
                /* streamWriter.WriteLine("开始下载Git"); streamWriter.Flush();
                 string[] fileNames = File.ReadAllText("D:\\Program Files\\SendMailServer\\batchExe.txt").Split(',');
                 streamWriter.WriteLine("bat地址" + fileNames[0]); streamWriter.Flush();
                 RunGit(fileNames[0], streamWriter);
                 streamWriter.WriteLine("处理bat完成"); streamWriter.Flush();

                 /*var form = new Form1();
                 form.Form1_Load(null, null);
                 form.LastSelectedDirectory = fileNames[2];
                 form.IsSelience = true;
                 form.ProcessDirectory();*/

                streamWriter.WriteLine("处理成功"); streamWriter.Flush();
                streamWriter.Dispose();
                streamWriter.Close();
                return;
            }
            catch (Exception err)
            {
                streamWriter.WriteLine(err.Message);
                streamWriter.WriteLine(err.StackTrace);
                streamWriter.Flush();
                streamWriter.Dispose();
                streamWriter.Close();
                return;
            }
        }
        public static string RunGit(string fileName, List<string> codes, StreamWriter streamWriter)
        {
            fileName = @"C:\Windows\System32\cmd.exe";
            ProcessStartInfo processStartInfo = new ProcessStartInfo(fileName);
            processStartInfo.UseShellExecute = false;
            processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
            processStartInfo.CreateNoWindow = false;
            processStartInfo.RedirectStandardOutput = false;
            processStartInfo.RedirectStandardInput = true;
            processStartInfo.RedirectStandardError = true;

            Process process = new Process();
            process.StartInfo = processStartInfo;
            process.Start();

            process.StandardInput.AutoFlush = true;
            for(var i=0; i < codes.Count; i++)
            {
                process.StandardInput.WriteLine(codes[i]);
                process.StandardInput.Flush();
            }

            process.WaitForExit();
            process.Close();
            return "";
        }
        public static string RunDownloadGit(string fileName, StreamWriter streamWriter)
        {
            var cmd = @"C:\Windows\System32\cmd.exe";
            ProcessStartInfo processStartInfo = new ProcessStartInfo(cmd);
            processStartInfo.UseShellExecute = false;
            processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
            processStartInfo.CreateNoWindow = false;
            processStartInfo.RedirectStandardOutput = false;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.RedirectStandardInput = true;
            processStartInfo.RedirectStandardError = true;


            Process process = new Process();
            process.StartInfo = processStartInfo;
            process.Start();
            process.StandardInput.WriteLine(fileName);
            process.StandardInput.Flush();

            process.WaitForExit();
            return "";
        }
        static void RunDownloadGit2(string fileName)
        {
            Process.Start(fileName);
        }
    }
}
